import Vue from 'vue'
import Router from 'vue-router'
import axios from 'axios'

import home from '../components/HomePage.vue';
import blog from '../components/Blog.vue';
import manage from '../components/Manage.vue';
import tag from '../components/Tag.vue';
import classify from '../components/Classify.vue';
import draft from '../components/Draft.vue';
import recovery from '../components/Recovery.vue';
import editBlog from '../components/EditBlog.vue';
import createBlog from '../components/CreateBlog.vue';
import getBlogList from '../components/BlogList.vue';
import getBlogDetail from '../components/BlogDetail.vue';
import search from '../components/Search.vue';
import login from '../components/Login.vue';
import error from '../components/Error.vue';
import index from '../components/Index.vue';
import blogPage from '../components/BlogPage.vue';
import classifyPage from '../components/ClassifyPage.vue';
import about from '../components/About.vue';

Vue.use(Router)

const router = new Router({
  // mode: 'history',
  routes: [{
    path: '/',
    redirect: '/index'
  },
  {
    path: '/index',
    component: index,
    redirect: '/index/home',
    children: [{
      path: 'home',
      component: home
    }, {
      path: 'blog',
      component: blogPage
    }, {
      path: 'classify',
      component: classifyPage
    }, {
      path: 'about',
      component: about
    }, {
      path: 'blogDetail/:id',
      component: getBlogDetail
    }]
  },
  {
    path: '/manage',
    component: manage,
    redirect: '/manage/blog/BlogList',
    children: [
    {
      path: 'blog',
      component: blog,
      children: [{
        path: 'edit/:id',
        component: editBlog,
      }, {
        path: 'create',
        component: createBlog
      }, {
        path: 'BlogList',
        component: getBlogList
      }, {
        path: 'BlogDetail/:id',
        component: getBlogDetail
      }]
    }, {
      path: 'search',
      component: search
    },
    {
      path: 'tag',
      component: tag
    },
    {
      path: 'classify',
      component: classify
    },
    {
      path: 'recovery',
      component: recovery
    },
    {
      path: 'draft',
      component: draft
    },
    ]
  },
  {
    path: '/login',
    component: login
  },
  {
    path: '/error',
    component: error
  }
  ]
});
//设置拦截器
router.beforeEach((to, from, next) => {
  if (to.path.startsWith('/login')) {
    window.localStorage.removeItem('access-Token');
    next();
  } else {
    if (to.path.startsWith('/manage')) {
      let User = JSON.parse(window.localStorage.getItem('access-User'));
      if (!User) {
        next({ path: '/login' });
      } else {
        axios({
          url: '/user/checkToken',
          method: 'get',
          headers: {
            token: window.localStorage.getItem('access-Token')
          }
        }).then((res) => {
          if (!res.data) {
            next({ path: '/error' });
          }
        })
        next();
      }
    }
    else {
      next();
    }
  }
});

export default router
